<template>
	<view style="padding: 0 30px;">
		<button class="button" type="primary" @tap="getPackageName">获取正在运行的应用</button>
		
		<button class="button" v-if="isAuth" type="primary">已开启权限，可以操作😁</button>
		<button class="button" v-else @click="skipSetting()" type="warn">未开启无障碍模式，点击开启</button>
		
		<view class="content" v-if="isAuth">
			<view class="">
				<text>X坐标</text>
				<input type="text" placeholder="x坐标" v-model="click_x" />
			</view>
			<view class="">
				<text>Y坐标</text>
				<input type="text" placeholder="Y坐标" v-model="click_y" />
			</view>
			
			<view class="">
				<text>延时时间(秒)</text>
				<input type="text" placeholder="Y坐标" v-model="click_sleep" />
			</view>
			<button class="button" type="primary" @tap="autoClick">开启单击</button>
			
			
			<view class="">
				<text>X坐标</text>
				<input type="text" placeholder="x坐标" v-model="click_x2" />
			</view>
			<view class="">
				<text>Y坐标</text>
				<input type="text" placeholder="Y坐标" v-model="click_y2" />
			</view>
			
			<view class="">
				<text>延时时间(秒)</text>
				<input type="text" placeholder="Y坐标" v-model="click_sleep2" />
			</view>
			<button class="button" type="primary" @tap="autoClick2">开启双击</button>
			
			
			<view class="">
				<text>X1坐标</text>
				<input type="text" placeholder="x坐标" v-model="swipe_x1" />
			</view>
			<view class="">
				<text>Y1坐标</text>
				<input type="text" placeholder="Y坐标" v-model="swipe_y1" />
			</view>
			<view class="">
				<text>X2坐标</text>
				<input type="text" placeholder="x坐标" v-model="swipe_x2" />
			</view>
			<view class="">
				<text>Y2坐标</text>
				<input type="text" placeholder="Y坐标" v-model="swipe_y2" />
			</view>
			<view class="">
				<text>延时时间(秒)</text>
				<input type="text" placeholder="Y坐标" v-model="swipe_sleep" />
			</view>
			<button class="button" type="primary" @tap="autoSwipe()">开启滑动</button>
			
			<view class="">
				<text>延时时间(秒)</text>
				<input type="text" placeholder="延时时间(秒)" v-model="keycode_sleep" />
			</view>
			<button class="button" type="primary" @tap="actionBack()">开启模拟返回键</button>
			
			<view class="">
				<text>包名</text>
				<input type="text" placeholder="x坐标" v-model="packageName" />
				<text>延迟时间</text>
				<input type="text" placeholder="x坐标" v-model="open_sleep" />
			</view>
			<button class="button" type="primary" @tap="openApp">开启自动返回</button>
			
			<button class="button" type="primary" @tap="clickListener">监听点击事件</button>
			
		</view>
		
	</view>
	
</template>

<script>
	// 获取 module 
	var _this;
	const hyy_click = uni.requireNativePlugin("hyy_click");
	const modal = uni.requireNativePlugin('modal');
	export default {
		data(){
			return {
				content: '未开启',
				isAuth: false,
				click_x: 400,
				click_y: 500,
				click_sleep: 5,
				
				click_x2: 400,
				click_y2: 500,
				click_sleep2: 5,
				
				swipe_x1: 0,
				swipe_y1: 1000,
				swipe_x2: 0,
				swipe_y2: 0,
				swipe_sleep: 6,
				
				keycode_sleep: 5,
				
				packageName: 'com.hyy.plugin',
				open_sleep: 8,
				
			}
		},
		
		onLoad() {
			_this = this;
	
		},
		
		onReady() {
			
		},
		
		onShow() {
			this.checkAuth(false);
		},
		
		onUnload() {
			
		},
		methods: {
			demo(){
				console.log('uni 执行了 demo');
				
			},
			
			checkAuth(bloo){
				console.log('检测权限');
				hyy_click.checkAuth(bloo, ret =>{
					console.log(ret);
					if(ret.code == 0)
					{
						_this.isAuth = true
					}
				})
			},
			
			skipSetting(){
				console.log('跳转权限');
				hyy_click.skipSetting(ret =>{
					console.log(ret);
				})
			},
			
			autoClick(){
				console.log('触发单击');
				
				hyy_click.autoClick({
					x: parseInt(this.click_x),
					y: parseInt(this.click_y),
					sleep: parseInt(this.click_sleep),
				}, ret=>{
					uni.showToast({
						title: JSON.stringify(ret),
						icon: 'none'
					})
					console.log(ret);
				})
				
			},
			
			autoClick2(){
				console.log('触发双击');
				
				hyy_click.autoClick2({
					x: parseInt(this.click_x2),
					y: parseInt(this.click_y2),
					sleep: parseInt(this.click_sleep2),
				}, ret=>{
					uni.showToast({
						title: JSON.stringify(ret),
						icon: 'none'
					})
					console.log(ret);
				})
				
			},
			
			/**
			 * 自动滑动
			 */
			autoSwipe(){
				console.log('触发滑动');
				
				hyy_click.autoSwipe({
					x1: parseInt(this.swipe_x1),
					y1: parseInt(this.swipe_y1),
					x2: parseInt(this.swipe_x2),
					y2: parseInt(this.swipe_y2),
					sleep: parseInt(this.swipe_sleep),
				}, ret=>{
					uni.showToast({
						title: JSON.stringify(ret),
						icon: 'none'
					})
					console.log(ret);
				})
			},
			
			actionBack(){
				console.log('触发按键');
				
				hyy_click.actionBack({
					sleep: parseInt(this.keycode_sleep),
				}, ret=>{
					uni.showToast({
						title: JSON.stringify(ret),
						icon: 'none'
					})
					console.log(ret);
				})
			},
			
			openApp(){
				console.log('打开APP');
				
				hyy_click.openApp({
					packageName: this.packageName,
					sleep: parseInt(this.open_sleep),
				}, ret=>{
					uni.showToast({
						title: JSON.stringify(ret),
						icon: 'none'
					})
					console.log(ret);
				})
			},
			
			getPackageName(){
				console.log('获取正在运行的应用');
				
				hyy_click.getPackageName(ret=>{
					uni.showToast({
						title: JSON.stringify(ret),
						icon: 'none'
					})
					console.log(ret);
				})
			},
			
			clickListener(){
				console.log('监听点击事件');
				
				hyy_click.clickListener(ret=>{
					uni.showToast({
						title: JSON.stringify(ret),
						icon: 'none'
					})
					console.log(ret);
				})
			},
			
			clickAll(){
				console.log('点击了页面');
			}
			
		}
	}
</script>


<style>
	.uni-padding-lr{background: #e33610;}
	.content{margin: 10rpx 50rpx;}
	.button{margin-bottom: 20rpx;}
</style>